package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BuyBean;
import com.project.bean.SupplierBean;
import com.project.mapper.ISupplierMapper;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;

    @Override
    public IPage<SupplierBean> cutFindByItem(Integer pageNum, String supplierName, String scope) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        if (supplierName != null && supplierName != ""){
            qw.like("su_name",supplierName);
        }
        if (scope != null && scope != ""){
            qw.like("su_range",scope);
        }
        return supplierMapper.selectPage(new Page<>(pageNum,2),qw);
    }

    @Override
    public SupplierBean findById(Integer supplierId) {
        return supplierMapper.selectById(supplierId);
    }

    @Override
    public void updateById(Integer supplierId, String newHead, String newTel) {
        System.out.println(newHead+"supplierId=============================");
        SupplierBean supplierBean = supplierMapper.selectById(supplierId);
        supplierBean.setHead(newHead);
        supplierBean.setTel(newTel);
        supplierMapper.updateById(supplierBean);
    }

    @Override
    public SupplierBean findByName(String supplierName) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        qw.eq("su_name",supplierName);
        return supplierMapper.selectOne(qw);
    }

    @Override
    public void add(SupplierBean supplierBean) {
        System.out.println(supplierBean.getName());
        supplierMapper.insert(supplierBean);
    }

    @Override
    public List<SupplierBean> findAll() {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        return supplierMapper.selectList(qw);
    }
}
